//
//  LikeOfVC.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "LikeOfVC.h"

#import <UIImageView+WebCache.h>

#import "VCtravles.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface LikeOfVC ()


@property (nonatomic,retain)UIView *backview;
@property (nonatomic,retain)UIImageView *image;

@property (nonatomic,retain)UILabel *declabel;

@property (nonatomic,retain)UIButton *crossYouji;

@property (nonatomic,retain)UIButton *xiazai;

@property (nonatomic,retain)UILabel *label;

@property (nonatomic,retain)UIView *showview;

@end

@implementation LikeOfVC


-(void)dealloc{
    
    [_label release];
    [_showview release];
    [_photo_url release];
    [_descript release];
    [_strtrip release];
    [_strxiazai release];
    [_backview release];
    [_image release];
    [_declabel release];
    [_crossYouji release];
    [_xiazai release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    self.navigationController.navigationBarHidden = YES;
    
    self.navigationController.navigationBar.translucent = YES;
    
    self.backview = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.backview.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.backview];
    
    [self.backview release];
    
    [self creattapGesture];
    
    [self creatbujian];
    
     [self hiddenStatusBar];
    
    
    [self creatpanGesture];
    
}
-(void)creatpanGesture{
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    
    [self.declabel addGestureRecognizer:pan];
    [pan release];


}


- (void)panAction:(UIPanGestureRecognizer *)pan{
    
    /** 更改 self.imageView.transform属性 */
    CGPoint point = [pan translationInView:pan.view];
    
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, 0, point.y);
    /** 参考点归零 */
     NSLog(@"%@",NSStringFromCGPoint(point));
    
    [pan setTranslation:CGPointZero inView:pan.view];
    NSLog(@"拖动操作");
    
}


-(void)creattapGesture{
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
    
    [tap release];
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap{
    NSLog(@"喜欢图片界面的点击事件");
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
}


-(void)hiddenStatusBar
{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

-(void)creatbujian{
    CGFloat width = self.backview.frame.size.width;
    CGFloat height = self.backview.frame.size.height;
    
    
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,width,width * self.height / self.width)];
    [self.view addSubview:self.image];
    
    self.image.backgroundColor = [UIColor redColor];
    
    self.image.center = self.backview.center;
    
    [self.image sd_setImageWithURL:[NSURL URLWithString:self.photo_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    
    [self.image release];
    
    
    if ([self.descript isEqual:[NSNull null]]){
       
        self.declabel = [[UILabel alloc]initWithFrame:CGRectMake(0, height - 30, width, 30)];
        [self.backview addSubview:self.declabel];
        
        self.declabel.backgroundColor = [UIColor blackColor];
        
        self.declabel.text = @"";
        [self.declabel release];


    }else{
        
        CGRect rect = [self.descript boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0]} context:nil];
        
        
        
        
        self.declabel = [[UILabel alloc]initWithFrame:CGRectMake(10, height - rect.size.height - 20, width - 20, rect.size.height)];
        [self.backview addSubview:self.declabel];
        
        [self.declabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        self.declabel.backgroundColor = [UIColor blackColor];
        
        self.declabel.alpha = 0.8;
        
        self.declabel.numberOfLines = 0;
        
        self.declabel.userInteractionEnabled  =YES;
        
        self.declabel.text = [NSString stringWithFormat:@"  %@",self.descript];
        
        self.declabel.textColor = [UIColor whiteColor];
        
        [self.declabel release];
 
        
        
    }
    
    
    
    self.crossYouji = [UIButton buttonWithType:UIButtonTypeCustom];
    self.crossYouji.frame = CGRectMake(width - 85,10, 35, 35);
    
    self.crossYouji.layer.cornerRadius = 5;
    
    [self.crossYouji setBackgroundImage:[UIImage imageNamed:@"tiaozhuan"] forState:UIControlStateNormal];
    
    self.crossYouji.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    
    [self.backview addSubview:self.crossYouji];
    
    [self.crossYouji addTarget:self action:@selector(crossYouji:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.xiazai = [UIButton buttonWithType:UIButtonTypeCustom];
    self.xiazai.frame = CGRectMake(width - 45, 10, 35, 35);
    
    self.xiazai.layer.cornerRadius = 5;
    
   self.xiazai.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    [self.backview addSubview:self.xiazai];
    
    [self.xiazai setBackgroundImage:[UIImage imageNamed:@"xiazai"] forState:UIControlStateNormal];
    
    [self.xiazai addTarget:self action:@selector(xiazai:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark  按键动作


-(void)crossYouji:(UIButton *)button{
    
    
    NSLog(@"进入游记");
    
    VCtravles *travel = [[VCtravles alloc]init];
   
    
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/%@.json",self.strtrip];
    NSLog(@"%@",str);
    travel.strURl = str;
    
    travel.modalPresentationStyle = UIModalPresentationFullScreen;
    
    travel.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    
    
    [self  presentViewController:travel animated:YES completion:^{
        
        
        
    }];

    [travel release];
    
    
}



-(void)xiazai:(UIButton *)button{
    
    NSLog(@"下载");
    
    
    
    
    
    UIImageWriteToSavedPhotosAlbum(self.image.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    
    self.showview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 130, 40)];
    self.showview.backgroundColor = [UIColor blackColor];
    

    
    self.showview.layer.cornerRadius = 10;
    
    [self.view addSubview:self.showview];
    
    self.showview.alpha = 1;
    
    self.showview.center = self.view.center;
    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 130, 40)];
    
    self.label.backgroundColor = [UIColor whiteColor];
    
    self.label.textAlignment = NSTextAlignmentCenter;
    
    self.label.text = @"成功保存到相册";
    
    self.label.layer.cornerRadius = 10;
    
    [self.showview addSubview:self.label];
    
    [self.showview release];
    [self.label release];
}


- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
        
       [UIView animateWithDuration:2 animations:^{
           
           self.showview.alpha = 0;
       } completion:^(BOOL finished) {
           
           
           [self.showview removeFromSuperview];
       }];
        
    }else
    {
        message = [error description];
    }
    NSLog(@"message is %@",message);
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
